<?php
/**
 * Minhlog 评论功能控制器
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 *
 * @category   Controller
 * @package    Frontend
 * @copyright  Copyright (c) 2009-2010 Minh.Shu@gmail.com
 * @license    New BSD License
 * @author     Minh.Shu
 * @version    $Id: CommentController.php 38 2010-05-11 13:53:04Z Minh.Shu $
 *
 */

class CommentController extends Minh_Controller_Action
{
    protected $_commentModel;
    protected $_articleModel;

    public function init ()
    {
        if(null === $this->_commentModel){
            if(class_exists('Domain_Model_Comment')){
                $this->_commentModel = new Domain_Model_Comment();
            } else {
                throw new Exception('文章评论模型类不存在!');
            }
        }

        if(null === $this->_articleModel){
            if(class_exists('Domain_Model_Article')){
                $this->_articleModel = new Domain_Model_Article();
            } else {
                throw new Exception('文章模型类不存在!');
            }
        }

        parent::init();
    }

    public function indexAction ()
    {
        if ($this->_request->isPost()) {
            $filter = new Zend_Filter_StripTags();
            $nickname = $filter->filter($this->_request->getPost('nickname'));
            $avatar = '/public/images/avatar/avatar.png';
            $comment = $filter->filter($this->_request->getParam('comment'));
            $commentPostId = (int) $this->_request->getPost('comment_post_id');
            $page = (int) $this->_request->getPost('comment_page_id');
            $captcha = md5(crypt($filter->filter($this->_request->getPost('captcha')), substr($filter->filter($this->_request->getPost('captcha')), 0, 2)));
            $capvalue = isset($_SESSION['captcha']['captchValue']) ? $_SESSION['captcha']['captchValue'] : '';

            if(!isset($captcha) || empty($captcha)){
                $info['msg'] = false;
                echo Zend_Json::encode($info);
            }

            if ($captcha != $capvalue) {
                $info['msg'] = false;
                echo Zend_Json::encode($info);
            } else {
                if ($nickname != '' && $comment != '' && $commentPostId > 0) {
                    $clientIp = parent::getIP();
                    $commentData = array('article_id' => $commentPostId, 'nickname' => $nickname , 'avatar' => $avatar , 'comment' => $comment , 'reply_time' => time() , 'ip' => $clientIp);
                    $commentAdd = $this->_commentModel->saveComment($commentData);

                    if ($commentAdd > 0) {
                        $commentCacheId = md5('detail_article_' . $commentPostId . '_comment' . '_' . $page);
                        $this->cache->remove($commentCacheId);
                        $articleData = array('comment_count' => new Zend_Db_Expr('comment_count+1'));
                        $articleCommentUpdate = $this->_articleModel->saveArticle($articleData,$commentPostId);
                        $articleCacheId = md5('detail_article_' . $commentPostId);
                        $this->cache->remove($articleCacheId);
                        /**
                         * 发送通知邮件给博主
                         * @uses Zend_Mail
                         */
                        try{
                            $bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');
                            $bootstrap->bootstrap('mail');
                            $this->mail = $bootstrap->getResource('mail');

                            $mail = new Zend_Mail('UTF-8');
                            $mail->setBodyHtml("您的博客有人发表了一篇新评论，快去看看吧！<br /><a href=\"http://".$this->view->siteDomain."/article/".$commentPostId."-1.html\" target=\"_blank\">点此查看详细内容！</a>");
                            $mail->setFrom($this->mail['personal']['email'], $this->view->siteName);
                            $mail->addTo($this->mail['personal']['email'], $this->mail['personal']['nickname']);
                            $mail->setSubject('您的博客有人发表新评论');
                            $mail->send();
                        } catch (Exception $e) {

                        }

                        unset($_SESSION['captcha']['captchValue']);
                        $info['msg'] = true;
                        echo Zend_Json::encode($info);
                    }
                }
            }
        }
    }
}